﻿using System;
using DM.DAO;
using DM.Entity;

namespace DM.Web.Admin
{
    public partial class ThôngTinThanhVien : System.Web.UI.Page
    {
        readonly static NsdDAO _nsdDao = new NsdDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindingForm();
            }
        }

        private void BindingForm()
        {
            Int32 id = Common.ParseInt32(Request.QueryString["id"]);
            NSD nsd = _nsdDao.Get(id);
            lbTenDangNhap.Text = nsd.Tendangnhap;
            lbHoTen.Text = nsd.Hoten;
            lbDiaChi.Text = nsd.Diachi;
            lbDienThoai.Text = nsd.Dienthoai;
            lbDiDong.Text = nsd.Didong;
            lbEmail.Text = nsd.Email;
            lbTaiKhoan.Text = nsd.Tendangnhap;
            divInf.Visible = true;
            divChP.Visible = false;
        }

        protected void btnChP_Click(object sender, EventArgs e)
        {
            divInf.Visible = false;
            divChP.Visible = true;
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("DanhSachThanhVien.aspx");
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            Int32 id = Common.ParseInt32(Request.QueryString["id"]);
            NSD nsd = _nsdDao.Get(id);
            string oldPW = txtOldPW.Text;
            if (nsd.Matkhau == Common.HashMD5(oldPW))
            {
                string newPW = txtNewPW.Text;
                nsd.Matkhau = newPW;
                _nsdDao.Update(nsd);
                Common.Show("Đổi mật khẩu thành công!", "DanhSachThanhVien.aspx");
            }
            else
            {
                Common.Show("Bạn nhập không đúng password!", Request.UrlReferrer.ToString());
            }
        }

        protected void btnThoat_Click(object sender, EventArgs e)
        {
            Response.Redirect("DanhSachThanhVien.aspx");
        }
    }
}
